package com.example.qinghe.frontController.communication;

import com.alibaba.fastjson.JSON;
import com.example.qinghe.entity.Common;
import com.example.qinghe.entity.communication.Login;
import com.example.qinghe.entity.communication.Staff;
import com.example.qinghe.service.communication.EnterpriseService;
import com.example.qinghe.service.communication.StaffService;
import com.example.qinghe.service.communication.UserService;
import com.example.qinghe.util.Md5Utils;
import com.example.qinghe.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * AdminController
 *
 * @author ruoyi
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/front/staff")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600, allowCredentials = "true")
//swagger

@Api(tags = "移动通讯录->人员信息", description = "FrontStaffController")
public class FrontStaffController {
    @Autowired
    private StaffService service;
    @Autowired
    private UserService userService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private RedisTemplate redisTemplate;
    @ApiOperation("员工登录")
    @PostMapping("/login")
    public Result login(@RequestBody Login staff,
                        HttpServletRequest request){
        Common common = new Common();
        common.put("phone",staff.getPhone()); common.put("pass", Md5Utils.hash(staff.getPass()));
        String header = userService.getHeader(request);
        if(header==null)
            return Result.error("请先获取请求头！");
        Staff login = service.login(common);
        if(login==null)
        return Result.error("手机号或密码有误！");
        System.out.println("login.isIsvaild():"+login.isIsvaild());
        if(login.isIsvaild()==false)
            return Result.error("您已被禁用！");
        login.setPass("");
        Long eid =login.getEnterprise_id();
        login.setBelong(service.getBelong(eid));
        Map map=new HashMap<>();
        map.put("staff",login);
        redisTemplate.opsForValue().set(header,map,30, TimeUnit.DAYS);
        System.out.println("Authorization:"+header);
        return Result.success(login,"登录成功");
    }
    @ApiOperation("查看人员信息：查找具体的人员信息")
    @GetMapping("/sel")
    public Result latestInfo(@ApiParam(name = "id", value = "员工id") @RequestParam(required = true) Long id
    ) {
        Map map = new HashMap<>();
        Common sel = service.sel(id);
        if (sel == null) {
            return Result.error("没有找到这个id");
        }
        Long eid =(Long) sel.get("enterprise_id");
        sel.put("belong",service.getBelong(eid));
        System.out.println("belong:"+service.getBelong(eid));
        map.put("my", sel);
        Common aLong1=null;
        if(sel.get("pid")!=null&&sel.get("pid")!="")
        {
            Long pid = ((BigInteger) sel.get("pid")).longValue();
            aLong1= service.sel(pid);
        }
        map.put("myup", aLong1);
        return Result.success(map);
    }

    @ApiOperation("下属：显示下属的人员信息")
    @GetMapping("/selsub")
    public Result selsub(HttpServletRequest request) {
        Staff staff = userService.getStaff(request);
        if(staff==null)
            return Result.error("请先登录！");
        Long id=staff.getId();
        //Long id=1L;

        Common common=new Common();
        common.put("pid",id);
        List<Common> commons = service.list2(common);
        return Result.success(commons);
    }


    @ApiOperation("根据关键词查看员工信息，这里的关键词只关联员工名字")
    @GetMapping("/selward")
    public Result selward(@ApiParam(name = "ward", value = "关键词") @RequestParam(required = true) String ward
    ) {
        Common common=new Common();
        List<Common> commons = service.selbyname(ward);
        for (int c = 0; c < commons.size(); c++) {
            System.out.println(JSON.toJSONString(commons.get(c)));
            Long eid =(Long) commons.get(c).get("enterprise_id");
            commons.get(c).put("belong",service.getBelong(eid));
        }
        return Result.success(commons);
    }

    @ApiOperation("同部门：显示同部门的人员信息")
    @GetMapping("/selcol")
    public Result selcol(HttpServletRequest request) {
        Staff staff = userService.getStaff(request);
        if(staff==null)
            return Result.error("请先登录！");
        Long id=staff.getId();
        //Long id=1L;
        Common common=new Common();
        Common sel = service.sel(id);
        common.put("enterprise_id",sel.get("enterprise_id"));
        List<Common> commons = service.list(common);
        return Result.success(commons);
    }

    @ApiOperation("转移数据，管理员用的")
    @GetMapping("/move")
    public void move()
    {
        List<Staff> enterprises=service.selAll();
        for (int i = 0; i < enterprises.size(); i++) {
            System.out.println(JSON.toJSONString(enterprises.get(i)));
            if(enterprises.get(i)!=null&&enterprises.get(i).getEnterprise_id()!=null)
            {
                enterprises.get(i).setId(enterprises.get(i).getId()+100);
                if(enterprises.get(i).getEnterprise_id()!=1)
                    enterprises.get(i).setEnterprise_id(enterprises.get(i).getEnterprise_id()+100);
                service.addStaff(enterprises.get(i));
            }

        }
    }


}
